package com.shenmazong.thread;

import java.util.concurrent.*;

/**
 * @program: server-java-demo
 * @description: ThreadPoolDemo2
 * @author: 亮子说编程
 * @create: 2020-11-11 13:35
 **/

class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
public class ThreadPoolDemo2 {

    public static void main(String[] args) {

        //--1
        doCreatePool1();

        //--2
        //doCreatePool1();

    }

    private static void doCreatePool1() {
        //ExecutorService service = Executors.newFixedThreadPool(4);
        //ExecutorService service = Executors.newCachedThreadPool();
        //ExecutorService service = Executors.newSingleThreadExecutor();
        //ExecutorService service = Executors.newScheduledThreadPool(4);

        // 2 + 10 + （10-2） = 20
        ExecutorService service = new ThreadPoolExecutor(2, 10, 10000L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());


        for (int i = 0; i < 100; i++) {
            service.execute(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":我要吃饭");
            });
        }

        //service.shutdownNow();
        //--1 放弃任务并抛出异常
        //ThreadPoolExecutor.AbortPolicy

        //--2
        //ThreadPoolExecutor.CallerRunsPolicy

        //ThreadPoolExecutor.DiscardOldestPolicy
        //ThreadPoolExecutor.DiscardPolicy

    }
}
